package com.misakyanls.examples;

import java.io.File;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.HashMap;

import nl.knaw.dans.common.dbflib.*;

public class TestDBF {

	public static void main(String[] args) {
		String[] greeting = new String[2];
		greeting[0] = "Welcome to Java DBF test";
		greeting[1] = "by Leon Misakyan";

		for (String g : greeting)
			System.out.println(g);

		ArrayList<Field> fields = new ArrayList<Field>();
		fields.add(new Field("STRNGFLD", Type.CHARACTER, 100));
		fields.add(new Field("DATEFLD", Type.DATE));
		fields.add(new Field("NUMFLD", Type.NUMBER, 3));

		Table table = null;
		try {
			table = new Table(new File("D:\\Temp\\report_1.dbf"),
					Version.DBASE_3, fields, "CP866");
			table.open(IfNonExistent.CREATE);

			HashMap<String, Value> map = new HashMap<String, Value>();
			map.put("STRNGFLD", new StringValue("���, �����, ����� c ���",
					"cp866"));
			map.put("DATEFLD", new DateValue(Calendar.getInstance().getTime()));
			map.put("NUMFLD", new NumberValue(123));

			Record record = new Record(map);
			table.addRecord(record);

		} catch (IOException e) {
			e.printStackTrace();
		} catch (DbfLibException e) {
			e.printStackTrace();
		}

		finally {
			try {
				table.close(); // don't forget to close it!
			} catch (IOException e) {
				e.printStackTrace();
			}
		}

	}

}
